package com.koron.order.common.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderExecute;
import com.koron.order.common.mapper.OrderExecuteMapper;
import com.koron.order.common.service.OrderExecuteService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (OrderExecute)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-03 16:27:32
 */
@Service("orderExecuteService")
public class OrderExecuteServiceImpl implements OrderExecuteService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderExecute orderExecute) {
        if (StringUtils.isEmpty(orderExecute.getId())) {
            orderExecute.setId(CodeTools.getCode32());
        }
        orderExecute.setCreateBy(getCurrUser().getAccount());
        orderExecute.setCreateByName(getCurrUser().getName());

        int i = factory.getMapper(OrderExecuteMapper.class).insert(orderExecute);
        return i;
    }

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderExecute orderExecute) {
        if (StringUtils.isEmpty(orderExecute.getId())) {
            orderExecute.setId(CodeTools.getCode32());
        }
        orderExecute.setCreateBy(getCurrUser().getAccount());
        orderExecute.setCreateByName(getCurrUser().getName());

        int i = factory.getMapper(OrderExecuteMapper.class).insertOrUpdate(orderExecute);
        return i;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderExecute> orderExecuteList) {

        if (CollectionUtils.isEmpty(orderExecuteList)) {
            return 0;
        }
        OrderExecuteMapper mapper = factory.getMapper(OrderExecuteMapper.class);
        mapper.deleteByOrderId(orderExecuteList.get(0).getOrderId());
        orderExecuteList.forEach(p -> {
            p.setCreateBy(getCurrUser().getAccount());
            p.setCreateByName(getCurrUser().getName());
            p.setCreateTime(new Date());
        });
        return mapper.insertBatch(orderExecuteList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderExecute orderExecute) {

        orderExecute.setUpdateBy(getCurrUser().getAccount());
        orderExecute.setUpdateByName(getCurrUser().getName());

        int i = factory.getMapper(OrderExecuteMapper.class).update(orderExecute);
        return i;
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = factory.getMapper(OrderExecuteMapper.class).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        int i = factory.getMapper(OrderExecuteMapper.class).deleteByOrderId(orderId);
        return i;
    }

    @Override
    @TaskAnnotation("deleteOrderExecute")
    public int deleteOrderExecute(SessionFactory factory, String orderId, String orderExecuteId) {
        int i = factory.getMapper(OrderExecuteMapper.class).deleteOrderExecute(orderId, orderExecuteId);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderExecute queryById(SessionFactory factory, String id) {

        OrderExecute orderExecute = factory.getMapper(OrderExecuteMapper.class).queryById(id);

        return orderExecute;
    }

    @Override
    @TaskAnnotation("queryMaxFactEndTimeByOrderId")
    public Date queryMaxFactEndTimeByOrderId(SessionFactory factory, String orderId) {

        Date factEndTime = factory.getMapper(OrderExecuteMapper.class).queryMaxFactEndTimeByOrderId(orderId);

        return factEndTime;
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderExecute> queryList(SessionFactory factory, OrderExecute orderExecute) {

        List<OrderExecute> list = factory.getMapper(OrderExecuteMapper.class).queryList(orderExecute);

        return list;
    }

}